/*
 * @Author: 吴华胜 765510616@qq.com
 * @Date: 2022-12-19 09:26:47
 * @LastEditors: 吴华胜 765510616@qq.com
 * @LastEditTime: 2023-03-23 17:42:31
 * @FilePath: \jshERP-web\src\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue'
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import notification from 'ant-design-vue/es/notification'
import { USER_ID, INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types'
import { generateIndexRouter } from '@/utils/util'
// import Pinyin from '@/utils/ChinesePY';
// import menus_text from '@/lang/menus';
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/user/login', '/user/register', '/user/register-result'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if (Vue.ls.get(USER_ID)) {
    /* has token */
    if (to.path === '/' || to.path === '/user/login') {
      next({ path: INDEX_MAIN_PAGE_PATH })
      NProgress.done()
    } else {
      if (store.getters.permissionList.length === 0) {
        store
          .dispatch('GetPermissionList')
          .then(res => {
            const menuData = res
            // 获取列表打印数据
            // const getPre = (res)=>{
            //   console.log('res :>> ', res);
            //   let arr =[]
            //   res.forEach(val=>{
            //     if(val.children && val.children.length){
            //       val.children.forEach(va=>{
            //         arr.push({
            //           text:va.text,
            //           py:Pinyin.GetJP(va.text) + '-list',
            //           en:menus_text[va.text]
            //         })
            //       })
            //     }else{
            //       arr.push({
            //         text:val.text,
            //         py:Pinyin.GetJP(val.text) + '-list',
            //         en:menus_text[va.text]
            //       })
            //     }
            //   })
            //   return arr
            // }
            // console.log('getPre(res) :>> ', JSON.stringify(getPre(res)));
            if (menuData === null || menuData === '' || menuData === undefined) {
              return
            }
            let constRoutes = []
            constRoutes = generateIndexRouter(menuData)
            // 添加主界面路由
            store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
              // 根据roles权限生成可访问的路由表
              // 动态添加可访问路由表
              router.addRoutes(store.getters.addRouters)
              const redirect = decodeURIComponent(from.query.redirect || to.path)
              next({ path: redirect })
            })
          })
          .catch(() => {
            store.dispatch('Logout').then(() => {
              next({ path: '/user/login' })
            })
          })
      } else {
        if (to.path) {
          _hmt.push(['_trackPageview', '/#' + to.fullPath])
        }
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next({ path: '/user/login' })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
